﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Threading;
using System.Timers;
using System.Diagnostics;

namespace GOA_Client
{
    public partial class ClientGUI : Form
    {
        public static SerialPort _serialPort;
        private int heartrate { get; set; }
        private int rpm { get; set; }
        private double speed { get; set; }
        private double distance { get; set; }
        private string time { get; set; }
        private int energy { get; set; }
        private int pulse { get; set; }
        private int power { get; set; }
        private Client c;
        public System.Timers.Timer timer;
        private Simulator sim = new Simulator();
        private string loginUsername;
        private int timerCounter = 0;
        public int exercisevalue = 25;
        //Exercise exercise;

        public ClientGUI()
        {
            loginForm lf = new loginForm(this);
            Application.Run(lf);
        }

        public void init(String loginUsername, int nummer)
        {
            this.loginUsername = loginUsername;
            Debug.WriteLine(loginUsername);
            timer = new System.Timers.Timer(3000);
            timer.Elapsed += new ElapsedEventHandler(timerElapsed);
            timer.Enabled = true;
            
            //Initializes the GUI
            InitializeComponent();
            _serialPort = new SerialPort();
            _serialPort.PortName = "COM0";
            _serialPort.BaudRate = 9600;
            //this.chatInputBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);

            foreach (string s in SerialPort.GetPortNames())
            {
                ComPorts.Items.Add(s);
            }


            Btn_Close.Enabled = false;
            Btn_Open.Enabled = true;

            c = new Client(this, nummer);
            Debug.WriteLine(c);
        }

        private void timerElapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                //string message = (string)c.ReadMessage();
                if (_serialPort.IsOpen)// && message != null)
                {
                    _serialPort.WriteLine("PW " + exercisevalue);
                    exercisevalue += 5;
                    string s = _serialPort.ReadLine();
                    
                    try
                    {
                        //if (timerCounter % 10 == 0)
                        //{
                            c.SendMessage(s);
                        //}
                            
                    }
                    catch (NullReferenceException)
                    {
                        //timer.Stop();
                    }
                }
                else if (!_serialPort.IsOpen)
                {
                    try
                    {
                        //if (timerCounter % 10 == 0)
                        //{
                            c.SendMessage(sim.RetrieveNewValues());
                        //}
                    }
                    catch (NullReferenceException)
                    {
                        //timer.Stop();
                    }
                }
            }
            catch (IOException ioex) { Debug.WriteLine(ioex); }
            catch (NullReferenceException) { }
            timerCounter++;
        }

        private void Btn_Open_Click(object sender, EventArgs e)
        {
            try
            {
                _serialPort.Open();
            }
            catch (IOException ioex) { Debug.WriteLine(ioex); }
            catch (UnauthorizedAccessException)
            {
                System.Windows.Forms.MessageBox.Show("Je hebt geen rechten tot deze COM-poort.");
            }
            try
            {
                if (_serialPort.IsOpen)
                {
                    Btn_Close.Enabled = true;
                    Btn_Open.Enabled = false;
                    _serialPort.WriteLine("CM");
                    _serialPort.ReadLine();
                    _serialPort.WriteLine("st");
                    _serialPort.WriteLine("st");
                    _serialPort.ReadLine();
                    //exercise = new Exercise(this, _serialPort);
                }
            }
            catch (IOException ioex) { Debug.WriteLine(ioex);
        }
            
     }

        public void stopAll() {
            c.stopClient();
        }


        private void ComPorts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_serialPort.IsOpen)
            {
                _serialPort.PortName = ComPorts.SelectedItem.ToString();
                Comlabel.Text = "Momentele com = " + ComPorts.SelectedItem.ToString();
            }
        }
        //

        private void Btn_Close_Click(object sender, EventArgs e)
        {
            try
            {
                _serialPort.Close();
            }
            catch (IOException ioex) { Debug.WriteLine(ioex); }

            if (!_serialPort.IsOpen)
            {
                Btn_Close.Enabled = false;
                Btn_Open.Enabled = true;
            }

        }

        
        /*
         * Adds zeroes in front of the patientID until it reaches a length of 5 characters.
         */
        private String ConfigurePatientID()
        {
            String ID = ""; //How do I connect this to the patientID from the database?
            //Checks if the ID is 5 characters long. If not, adds an additional zero in front.
            while (ID.Length < 5)
            {
                ID = "0" + ID;
            }
            return ID;
        }

        private void ClientGUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            c.SendMessage("terminate");
            Environment.Exit(0);
        }
    }
}
